﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace KCL.Code.Data
{
    public class ProductDAO : BaseDAO
    {
        public List<Product> GetAllProducts()
        {
            try
            {
                KCLDataContext ctx = new KCLDataContext();
                var lstProducts = from p in ctx.Products
                                  where p.isActive == true
                                  select p;
                return lstProducts.AsEnumerable().ToList();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public bool Insert(Product p)
        {
            try
            {
                KCLDataContext ctx = new KCLDataContext();
                ctx.Products.InsertOnSubmit(p);
                ctx.SubmitChanges();
                return true;
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                if (ex.Number == 2627)
                {
                    try
                    {
                        KCLDataContext ctx = new KCLDataContext();
                        var productToRestore = (from pro in ctx.Products
                                                where pro.code.ToUpper() == p.code.ToUpper()
                                                && pro.isActive == false
                                                select pro).FirstOrDefault();
                        productToRestore.code = p.code;
                        productToRestore.name = p.name;
                        productToRestore.isActive = true;
                        ctx.SubmitChanges();
                        return true;
                    }
                    catch (Exception)
                    {
                        return false;
                    }
                }
                return false;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Update(Product p)
        {
            try
            {
                KCLDataContext ctx = new KCLDataContext();
                var productToUpdate = (from pro in ctx.Products
                                       where pro.id == p.id
                                       select pro).FirstOrDefault();
                productToUpdate.code = p.code.Trim();
                productToUpdate.name = p.name.Trim();
                productToUpdate.isActive = p.isActive;
                if (productToUpdate.code.Length == 0 ||
                    productToUpdate.name.Length == 0)
                    return false;
                ctx.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Delete(Product p)
        {
            try
            {
                KCLDataContext ctx = new KCLDataContext();
                var productToDelete = (from pro in ctx.Products
                                       where pro.id == p.id
                                       select pro).FirstOrDefault();
                productToDelete.isActive = false;
                ctx.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public string GetNextProductCode()
        {
            try
            {
                KCLDataContext ctx = new KCLDataContext();
                return ctx.GetNextProductCode();
            }
            catch
            {
                return string.Empty;
            }
        }
    }
}